package com.test.ebond3.testproxool;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import oracle.jdbc.pool.OracleDataSource;

public class ConnectionDemo {

    public static void main(String[] args) throws SQLException, PropertyVetoException, InstantiationException, IllegalAccessException, ClassNotFoundException {
        System.out.println("使用连接池................................");
        for (int i = 0; i < 20; i++) {
            long beginTime = System.currentTimeMillis();
            Connection conn = ConnectionManager.getInstance().getConnection();
            try {
                PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM contact");
                ResultSet rs = pstmt.executeQuery();
                while (rs.next()) {
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }

            long endTime = System.currentTimeMillis();
            System.out.println("第" + (i + 1) + "次执行花费时间为:" + (endTime - beginTime));
        }

        System.out.println("不使用连接池................................");
        for (int i = 0; i < 20; i++) {
            long beginTime = System.currentTimeMillis();
//            ComboPooledDataSource dataSource = new ComboPooledDataSource();
//            dataSource.setUser("root");
//            dataSource.setPassword("");
//            dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/monitor?useUnicode=true&amp;characterEncoding=utf-8");
//            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            Class.forName("com.mysql.jdbc.Driver").newInstance();
            Connection conn=java.sql.DriverManager.getConnection("jdbc:mysql://127.0.0.1:3306/monitor?useUnicode=true&amp;characterEncoding=utf-8","root","");

           // dataSource.setURL("jdbc:mysql://127.0.0.1:3306/monitor?useUnicode=true&amp;characterEncoding=utf-8");
            //Connection conn = dataSource.getConnection();
            try {
                PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM contact");
                ResultSet rs = pstmt.executeQuery();
                while (rs.next()) {
                                    // do nothing...
                }
            } catch (SQLException e) {
                e.printStackTrace();
            } finally {
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            long endTime = System.currentTimeMillis();
            System.out.println("第" + (i + 1) + "次执行花费时间为:"
                                + (endTime - beginTime));
        }

    }
}
